import React, { Component } from 'react';
import { Link, withRouter } from 'react-router-dom';
import { Balloon } from '@icedesign/base';
import Menu, { SubMenu, Item as MenuItem } from '@icedesign/menu';
import headerMenuConfig from '../../menuConfig';
import axios from 'axios';
import { GetUrlParam, LoginReq } from '../../baseConfig';
import './Header.scss';

@withRouter

export default class Header extends Component {
  static propTypes = {};

  static defaultProps = {};

  constructor(props) {
    super(props);
    this.state = {
      token:'',
      secu_abbr:'',
      secu_code:'',
      company_full_name:'',
      market:'',
      superUser:false,
      // secuCode:'000000111',
    };
  }

  componentWillMount(){
    // let sc = GetUrlParam("secu_code");
    localStorage.setItem("TOKEN",GetUrlParam("secu_code"))
    this.setState({
      token:GetUrlParam("secu_code")
    })
    if(localStorage.getItem("TOKEN")==null){
      window.location.href = `${global.constants.LOCALHOST}`
      }else{
        //验证TOKEN
        // console.log(`${this.GetUrlParam("secu_code")}`)
        axios.get(`${global.constants.GOLDEYEGETHOST}cattle/auth_token?token=${localStorage.getItem("TOKEN")}`)
          .then(data => {
            // console.log("请求验证",data.data.flag);
            if(data.data.flag){
                //请求基础数据
                this.getCompanyInfo(localStorage.getItem("TOKEN"))
                this.checkAuthor(GetUrlParam("secu_code"));
            }else{
              window.location.href = `${global.constants.LOCALHOST}`
            }
        })
        .catch(function (error) {
            console.log(error);
        });
    }
  }

  componentDidMount(){
    // console.log("Did",this.state.secu_abbr)
  }

  GetUrlParam(paraName)  {
  　var url = document.location.toString();
      // console.log("url",url)
  　var arrObj = url.split("?");
　　　if (arrObj.length > 1) {
　　　　　var arrPara = arrObj[1].split("&");
　　　　　var arr;
　　　　　for (var i = 0; i < arrPara.length; i++) {
　　　　　　　arr = arrPara[i].split("=");
　　　　　　　if (arr != null && arr[0] == paraName) {
　　　　　　　　　return arr[1];
　　　　　　　}
　　　　　}
　　　　　return "";
　　　}
　　　else { return ""; }
  }

  //获取头部公司信息
  getCompanyInfo(sc) {
    axios.get(`${global.constants.GOLDEYEGETHOST}cattle/get_company_info?secu_code=${sc}`)
    .then(data => {
        // console.log("请求数据",sc);
        if(data.data.flag){
            // localStorage.setItem("TOKEN",sc),
            localStorage.setItem("secu_code",data.data.data.secu_code),
            localStorage.setItem("secu_abbr",data.data.data.secu_abbr),
            localStorage.setItem("company_full_name",data.data.data.company_full_name),
            localStorage.setItem("market",data.data.data.market)
            this.setState({
              secu_code:data.data.data.secu_code,
              secu_abbr:data.data.data.secu_abbr,
              company_full_name:data.data.data.company_full_name,
              market:data.data.data.market,
            })
        }else{
          // window.location.href = `${global.constants.LOCALHOST}`
        }
    })
    .catch(function (error) {
        console.log(error);
    });
  }

  //获取是否超级用户
  checkAuthor(sc){
    // console.log(sc)
    axios.get(`${global.constants.GOLDEYEGETHOST}cattle/get_account_type?secu_code=${sc}`)
    .then(data => {
      // console.log("用户等级1",data.data.data.is_super_account)
      // if(data.data.flag){
        this.setState({
          superUser: data.data.data.is_super_account
        },() => {
          localStorage.setItem("superUser",data.data.data.is_super_account)
          console.log("超级用户",data.data.data.is_super_account)
        })
      // }
  })
  .catch(function (error) {
      console.log(error);
  });
  }

  // LoginReq (sc) {
  //   let that = this;
  //   if(GetUrlParam("secu_code"==null)){
  //     window.location.href = `${global.constants.LOCALHOST}`
  //   }else{
  //       //验证TOKEN
  //       // console.log(`${this.GetUrlParam("secu_code")}`)
  //       axios.get(`https://goldeye.cfbond.com/cattle/auth_token?token=${sc}`)
  //       .then(function (data){
  //           console.log("请求验证",data.data.flag);
  //           if(data.data.flag){
  //               //请求基础数据
  //               that.getCompanyInfo(sc)
  //           }else{

  //           }
  //       })
  //       .catch(function (error) {
  //           console.log(error);
  //       });
  //   }
  // }

  quit(){
    console.log("quit")
  }

  GetUrlString(paraName) {
  　var url = document.location.toString();
    if(url.indexOf("#/earlyWarning")!=-1){
      // console.log("/earlyWarning");
      return "/earlyWarning"
    }else if(url.indexOf("#/Evaluate")!=-1){
      // console.log("/Evaluate");
      return "/Evaluate"
    }else if(url.indexOf("#/report")!=-1){
      // console.log("/report");
      return "/report"
    }else if(url.indexOf("#/service")!=-1){
      // console.log("/service");
      return "/service"
    }else if(url.indexOf("#/statute")!=-1){
      // console.log("/statute");
      return "/statute"
    }else if(url.indexOf("#/topNews")!=-1){
      return "/topNews"
    }else if(url.indexOf("#/tool") !=-1){
      return "/tool"
    }else{
      return "/"
    }
  }

  jumpHtml(url){
    window.history.href = url;
  }

  handleNavClick(key){
    // console.log("key",key)
    if(key==="/earlyWarning"){
      // console.log(`${global.constants.GOLDEYELOCALHOST}gdBeta2/#/earlyWarning/creditRisk?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.GOLDEYELOCALHOST}gdBeta2/#/earlyWarning/creditRisk?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/Evaluate"){
      // console.log(`${global.constants.GOLDEYELOCALHOST}gdBeta2/#/evaluate/xpCompliance?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.GOLDEYELOCALHOST}gdBeta2/#/evaluate/xpCompliance?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/report"){
      // console.log(`${global.constants.LOCALHOST}report/reportList?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.LOCALHOST}report/reportList?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/service"){
      // console.log(`${global.constants.LOCALHOST}provideServices/servicesList?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.LOCALHOST}provideServices/servicesList?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/favorite"){
      // console.log(`${global.constants.LOCALHOST}law/list?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.LOCALHOST}law/list?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/topNews"){
      // console.log(`${global.constants.LOCALHOST}wealthheadlines/listcontent?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.LOCALHOST}wealthheadlines/listcontent?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/tool"){
      // console.log(`${global.constants.GOLDEYELOCALHOST}gdBeta2/#/tools/marketAnalysis?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.GOLDEYELOCALHOST}gdBeta2/#/tools/marketAnalysis?secu_code=${localStorage.getItem("TOKEN")}`
    }else if(key==="/home"){
      // console.log(`${global.constants.LOCALHOST}home/to_homepage?secu_code=${localStorage.getItem("TOKEN")}`)
      return `${global.constants.LOCALHOST}home/to_homepage?secu_code=${localStorage.getItem("TOKEN")}`
    }
  }

  render() {
    const { location = {} } = this.props;
    const { pathname } = location;
    return (
      <div className="header-container">
        <div className="header-content">
          <div className="header-navbar">
            <div className="nav-top">
              <div style={{width:'1200px',margin:'0 auto'}}>
              <div className="left-top">
                  <img
                    src='./assetc/logo.png'
                    style={styles.logoImg}
                    alt="logo"
                  />
                  <span className="logotext">{this.state.secu_code} ({this.state.secu_abbr})</span>
                  {/* <span className="logotext">{localStorage.getItem("secu_code")} ({localStorage.getItem("secu_abbr")})</span> */}
                {/* <Logo company={this.state.secu_abbr} secuCode={this.state.secu_code} /> */}
              </div>
              <div className="right-top">
                  {/* <a href="https://eye.cfbond.com/favorite/list"> */}
                  <a href={global.constants.LOCALHOST+'favorite/list'}>
                    <div className="icon-collection"></div>
                  </a>
                  {/* <a href="https://eye.cfbond.com/message/list"> */}
                  <a href={global.constants.LOCALHOST+'message/list'}>
                    <div className="icon-message"></div>
                  </a>
                  <Balloon
                    trigger={
                      <div className="icon-system"></div>
                    }
                    triggerType='hover'
                    closable={false}
                    align='br'
                    className="icon-system"
                  >
                    <ul className="uu">
                      { /*!this.state.superUser && <li className="icon-collection">
                        {<a href="https://eye.cfbond.com/user/list">}
                        <a href={global.constants.LOCALHOST+'user/list'}>
                          用户管理
                        </a>
                      </li> */}
                      { !this.state.superUser && <li className="icon-message">
                        {/* <a href="https://eye.cfbond.com/order/list"> */}
                        <a href={global.constants.LOCALHOST+'order/list'}>
                          订单管理
                        </a>
                      </li> }
                      { !this.state.superUser && <li className="icon-system">
                        {/* <a href="https://eye.cfbond.com/sys_manage/sys_manage"> */}
                        <a href={global.constants.LOCALHOST+'sys_manage/sys_manage'}>
                          系统管理
                        </a>
                      </li> }
                      { !this.state.superUser && <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/personalInfo/detail"> */}
                        <a href={global.constants.LOCALHOST+'personalInfo/detail'}>
                          我的设置
                        </a>
                      </li> }
                      <li className="icon-user">
                        <a href="#">
                          帮助中心
                        </a>
                      </li>
                      { this.state.superUser && <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/superAccountLinkInfo/list"> */}
                        <a href={global.constants.LOCALHOST+'superAccountLinkInfo/list'}>
                          公司切换
                        </a>
                      </li>}
                      {/* onClick={this.quit.bind(this)} */}
                      <li className="icon-user">
                        <a href={global.constants.LOCALHOST}>
                          退出
                        </a>
                      </li>
                    </ul>
                  </Balloon>
                  
                  { !this.state.superUser && <Balloon
                    trigger={
                      <div className="icon-user"></div>
                    }
                    triggerType='hover'
                    closable={false}
                    align='br'
                    className="icon-user"
                  >
                    <ul>
                      <li className="icon-collection">
                        {/* <a href="https://eye.cfbond.com/company_info/info"> */}
                        <a href={global.constants.LOCALHOST+'company_info/info'}>
                          公司基本信息
                        </a>
                      </li>
                      <li className="icon-system">
                        {/* <a href="https://eye.cfbond.com/staff_info/list"> */}
                        <a href={global.constants.LOCALHOST+'staff_info/list'}>
                          董监高任职
                        </a>
                      </li>
                      <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/main_products/list"> */}
                        <a href={global.constants.LOCALHOST+'main_products/list'}>
                          主营产品
                        </a>
                      </li>
                      <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/affiliated_company/list"> */}
                        <a href={global.constants.LOCALHOST+'affiliated_company/list'}>
                          关联公司设置
                        </a>
                      </li>
                      <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/competition_company/list"> */}
                        <a href={global.constants.LOCALHOST+'competition_company/list'}>
                          竞争对手设置
                        </a>
                      </li>
                      <li className="icon-user">
                        {/* <a href="https://eye.cfbond.com/sensitive_period/list"> */}
                        <a href={global.constants.LOCALHOST+'sensitive_period/list'}>
                          敏感期窗口设定
                        </a>
                      </li>
                    </ul>
                  </Balloon>}
                               
                {/* <ul>
                  <li className="icon-collection"></li>
                  <li className="icon-message"></li>
                  <li className="icon-system"></li>
                  <li className="icon-user"></li>
                </ul> */}
              </div>
              </div>
            </div>

            {/* <div className="line"></div> */}
            <div className="nav-list">
              <Menu
                className="header-navbar-menu"
                // onClick={this.handleNavClick.bind(this)}
                selectedKeys={[this.GetUrlString()]}
                defaultSelectedKeys={[this.GetUrlString()]}
                // openKeys={[pathname]}
                mode="horizontal"
              >
                {headerMenuConfig &&
                  headerMenuConfig.length > 0 &&
                  headerMenuConfig.map((nav, index) => {
                    // if (nav.children && nav.children.length > 0) {
                    //   return (
                    //     <SubMenu
                    //       triggerType="click"
                    //       key={index}
                    //       title={
                    //         <span>
                    //           <span>{nav.name}</span>
                    //         </span>
                    //       }
                    //     >
                    //       {nav.children.map((item) => {
                    //         const linkProps = {};
                    //         if (item.external) {
                    //           if (item.newWindow) {
                    //             linkProps.target = '_blank';
                    //           }

                    //           linkProps.href = item.path;
                    //           return (
                    //             <MenuItem key={item.path}>
                    //               <a {...linkProps}>
                    //                 <span>{item.name}</span>
                    //               </a>
                    //             </MenuItem>
                    //           );
                    //         }
                    //         linkProps.to = item.path;
                    //         return (
                    //           <MenuItem key={item.path}>
                    //             <Link {...linkProps}>
                    //               <span>{item.name}</span>
                    //             </Link>
                    //           </MenuItem>
                    //         );
                    //       })}
                    //     </SubMenu>
                    //   );
                    // }
                    const linkProps = {};
                    // if (nav.external) {
                    //   if (nav.newWindow) {
                    //     linkProps.target = '_blank';
                    //   }
                    //   linkProps.href = nav.path;
                    //   return (
                    //     <MenuItem key={nav.path}>
                    //       <a {...linkProps}>
                    //         <span>
                    //           {nav.name}
                    //         </span>
                    //       </a>
                    //     </MenuItem>
                    //   );
                    // }
                    // linkProps.to = nav.path;
                    return (
                      // onClick={this.onClick.bind(this,pathname)}
                      <MenuItem key={nav.path} selectedKeys={[pathname]}>
                        {/* <Link
                          // {...linkProps}
                          to={{
                            pathname:`${this.handleNavClick(nav.path)}`,
                            // pathname: `${nav.path}`,
                            // search: `?secu_code=${localStorage.getItem("TOKEN")}`,
                          }}
                          replace
                        > */}
                        <a href={`${this.handleNavClick(nav.path)}`}>
                          {nav.name}
                        </a>
                        {/* </Link> */}
                      </MenuItem>
                    );
                  })}
              </Menu>
            </div>
            
          </div>
        </div>
      </div>
    );
  }
}

const styles = {
  logoImg: {
    width: '100px',
    padding: '15px 0'
  },
  logoText: {
    display: 'block',
    // maxWidth: '120px',
    // overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap',
    marginLeft: '10px',
    paddingLeft: '10px',
    verticalAlign:'10px',
    fontSize: '24px',
    lineHeight:'64px',
    color: '#d8a874',
    // fontWeight: 'bold',
  },
  // logoText: {
  //   display: 'block',
  //   // maxWidth: '120px',
  //   // overflow: 'hidden',
  //   textOverflow: 'ellipsis',
  //   whiteSpace: 'nowrap',
  //   marginLeft: '10px',
  //   fontSize: '24px',
  //   color: '#D8A874',
  //   fontWeight: 'bold',
  // },
  uu:{
    textAlign:'left'
  },
  aa:{
    color:''
  }
};
